/**
 *
 *  Copyright (c) 2011, AstroDAbis
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without modification,
 *  are permitted provided that the following conditions are met:
 *
 *      * Redistributions of source code must retain the above copyright notice,
 *        this list of conditions and the following disclaimer.
 *      * Redistributions in binary form must reproduce the above copyright notice,
 *        this list of conditions and the following disclaimer in the documentation
 *        and/or other materials provided with the distribution.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 *  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 *  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 *  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 *  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 *  OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
package org.jiscinvolve.astrodabis.mallow.impl.model.vot;

import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import uk.ac.starlink.table.StarTable;
import uk.ac.starlink.table.ColumnInfo;
import uk.ac.starlink.table.RowSequence;

import org.jiscinvolve.astrodabis.mallow.model.api.JoinTagData;
import org.jiscinvolve.astrodabis.mallow.model.api.JoinTagParser;

/**
 * JoinTagParser that reads data from a StarTable.
 * @todo refactor as Iterator<JoinTag>
 * @todo separate next() into next() and step() methods
 *
 */
public abstract class StarTableJoinTagParser
implements JoinTagParser, JoinTagData
    {
    /**
     * Our debug logger.
     * 
     */
    private static Log log = LogFactory.getLog(StarTableJoinTagParser.class);

    /**
     * Public constructor.
     *
     */
    public StarTableJoinTagParser()
        {
        super();
        }

    /**
     * Process a StarTable resource.
     *
     */
    protected void init(StarTable table)
    throws IOException
        {
        log.debug("StarTableJoinTagParser.init(StarTable)");
        //
        // Get the table rows.
        this.rows = table.getRowSequence();
        //
        // Step to the first row.
        this.next();
        }

    /**
     * Our StarTable row sequence.
     *
     */
    protected RowSequence rows ;

    /**
     * Our next row of data.
     *
     */
    protected Object[] next ;
     
    /**
     * Our current row of data.
     *
     */
    protected Object[] data ;

    @Override
    public boolean hasNext()
        {
        return (next != null) ;
        }

    // This should be step(), leaving next() to be implemented using Row and Join factories.
    @Override
    public JoinTagData next()
        {
        log.debug("StarTableJoinTagParser.next()");

        data = next ;
        next = null ;

        try {
            if (rows != null)
                {
                if (rows.next())
                    {
                    next = rows.getRow();
                    }
                }
            return this ;
            }
        catch (IOException ouch)
            {
            log.error("IOException while processing input data [" + ouch.getMessage() + "]");
// What should we return ?
// We need to prevent the process using our Iterator from continuing.
// 'this' will contain bad data, but if hasNext() returns true, then 'null' isn't part of the Iterator contract.
            return null ;
            }
        }

    @Override
    public void remove()
        {
        throw new UnsupportedOperationException(
            "Iterator.remove() not supported"
            );
        }
    }

